var _CONFIG = {
    TYPE: '',
    BINDING_URI: base_url + 'cms/so/config/binding/',
    EDITPANEL_URI: base_url + 'cms/so/config/editpanel/',
    COMMIT_URI: base_url + 'cms/so/config/oncommit/',
    DELETE_URI: base_url + 'cms/so/config/ondelete/'
};
var _SO = (function() {
    var oTable;
    var that = this;
    return {
        createtable: function() {
            oTable = $('#data_table').dataTable({
                aaSorting: [],
                aoColumns: [{
                        mData: "Id",
                        sWidth: "60px",
                        sClass: 'gridAction',
                        mRender: function(value, type, datarow) {
                            var str = '';
                            str += '<ul class="table-controls">';
                            //str += '<li><a href="#" class="tip" onclick="" title="Add entry" ><i class="icon-plus"></i></a> </li>'+
                            str += '<li><a href="#" class="tip" onclick="_SO.onEditItem(' + value + ')" title="Edit entry" ><i class="icon-pencil"></i></a> </li>';
                            if (datarow.Lock != 1)
                                str += '<li><a href="#" class="tip" onclick="_SO.onDeleteItem(' + value + ')" title="Remove entry" ><i class="icon-trash"></i></a> </li>';
                            str += '</ul>';
                            return str;
                        }
                    }, {
                        mData: "Title", sWidth: "25%"
                    }, {
                        mData: "Value",
                        mRender: function(value, type, datarow) {
                            var str = '<div style="max-height:120px;overflow:hidden">';
                            str += value;
                            str += '</div>';
                            return str;
                        }
                    }, {
                        mData: "Type", bVisible: false
                    },

                    {
                        mData: "Insert",
                        sWidth: "120px",
                        bVisible: false
                    }, {
                        mData: "Update",
                        sWidth: "120px",
                        bVisible: false
                    }, {
                        mData: "Lock",
                        sWidth: "40px",
                        bVisible: false
                    }
                ],
                sServerMethod: "POST",
                iDisplayLength: 50,
                bProcessing: true,
                bServerSide: true,
                bSort: false,
                bPaginate: false,
                "bFilter": false,
                "bSearchable":false,
                "bInfo":false,
                sAjaxSource: _CONFIG.BINDING_URI + _CONFIG.TYPE,
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    sSearch: "<span>Filter records:</span> _INPUT_",
                    sLengthMenu: "<span>Show entries:</span> _MENU_",
                    oPaginate: {
                        "sFirst": "First",
                        "sLast": "Last",
                        "sNext": ">",
                        "sPrevious": "<"
                    }
                }
            });
            oTable.fnSetFilteringDelay(2000);
        },
        onInit: function() {
            this.createtable();
        },
        onAddItem: function() {
            this.onEditItem(0);
        },
        onEditItem: function(Id) {
            if (PENDING)
                return false;
            $('#dialog-edit-panel>.base-dialog-modal').html('<div class="base_loading" style="display: block;height: 60px"></div>');
            bckdialog({
                title: 'Add/Edit',
                message: $('#dialog-edit-panel'),
                buttons: [{
                    text: 'Done',
                    'class': 'base_button base_green base-small-border-radius',
                    click: function() {
                        _SO.onSave();
                    }
                }, {
                    text: 'Close',
                    'class': 'base_button base_red base-small-border-radius',
                    click: function() {
                        $(this).dialog("close");
                    }
                }]
            }).open();
            backend({
                url: _CONFIG.EDITPANEL_URI + _CONFIG.TYPE,
                data: {
                    Id: Id
                },
                callback: function(rsdata) {
                    if (rsdata.result < 0) {
                        $('#dialog-edit-panel>.base-dialog-modal').html('<span style="color:red">' + rsdata.message + '</span>');
                    } else {
                        $('#dialog-edit-panel>.base-dialog-modal').html(rsdata.htmlreponse);
                        $('#dialog-edit-panel').dialog('close').dialog('open');
                        $('#_form_edit_panel select').select2();
                        addEditorBasic('Value');
                    }
                }
            }).call();
        },
        onSave: function() {
            $('#Value').val(CKEDITOR.instances['Value'].getData());
            if ($('#_form_edit_panel').validationEngine('validate') === false) return false;
            var Params = $('#_form_edit_panel').serializeObject();
            $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').show();
            new backend({
                url: _CONFIG.COMMIT_URI,
                data: {
                    Params: Params,
                    Id: $('#Id').val()
                },
                callback: function(rsdata) {
                    $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').hide();
                    if (rsdata.result < 0) {
                        bckdialog({
                            type: 'error'
                        }).open(rsdata.message);
                    } else {
                        _SO.onRefresh();
                        _SO.onCancel();
                    }

                }
            }).call();
            return false;
        },
        onCancel: function() {
            $('#dialog-edit-panel').dialog('close');
        },
        onDeleteItem: function(Id) {
            backend_delete(_CONFIG.DELETE_URI, Id, function() {
                _SO.onRefresh();
            });
        },
        onRefresh: function() {
            oTable.fnDraw(false);
        },
        onFillter: function(v) {
            oTable.fnFilter(v, 2);
        },
        onStatusChange: function(that, Id) {
            var _status = $(that).is(':checked') ? 'true' : 'false';
            new backend({
                url: _CONFIG.COMMIT_URI,
                data: {
                    Params: {
                        staf_status: _status
                    },
                    Id: Id
                },
                callback: function(rsdata) {
                    if (rsdata.result < 0) {
                        bckdialog({
                            type: 'error'
                        }).open(rsdata.message);
                    }
                }
            }).call();
        },
        onChangeSource: function(type) {
            if (type !== _CONFIG.TYPE) {
                _CONFIG.TYPE = type;
                oTable.fnSettings().sAjaxSource = (_CONFIG.BINDING_URI + _CONFIG.TYPE);
                oTable.fnDraw(false);
            }
        }
    };
}());